/*
 * @Descripttion:
 * @Author: xp
 * @Date: 2022-09-02 16:59:44
 */
import Vue from 'vue'

import 'normalize.css/normalize.css' // css重置样式
import './assets/iconfont/iconfont.css' // css图标引进

import Element from 'element-ui' // Element
import zhLocale from 'element-ui/lib/locale/lang/zh-CN' // element-ui 中文
import './styles/element-variables.scss' // element-variables通用样式
import '@/styles/index.scss'
import App from './App'
import store from './store'
import router from './router'
import api from "./api/index";
import commonFn from './utils/public'
import common from './modules/common'
import dayjs from 'dayjs'
import VueClipboard from 'vue-clipboard2' // 复制到粘贴板
Vue.use(VueClipboard)
// 校验整合
import validator from '@/utils/validator'
// 正则表达式
import _regExp from '@/utils/regExp'
// import './permission' // permission control
import './utils/error-log' // error log
// 挂载vue原型链全局方法
Vue.prototype.$api = api;
Vue.prototype.$common = {...commonFn, ...common};
Vue.prototype.$systemId = '102054351144484884' // 系统标识id
import VueDragscroll from "vue-dragscroll";
Vue.use(VueDragscroll)
Vue.directive("double",{
  bind: function (el, binding, prevVnode) {
    el.addEventListener("click", (e) => {
      el.classList.add("is-disabled");
      el.disabled = true;
      let box = document.createElement("i");
      box.setAttribute("class", "el-icon-loading");
      box.setAttribute("id", "showLoading");
      let lastNode = el.childNodes[el.childNodes.length - 1];
      el.insertBefore(box, lastNode);
      setTimeout(() => {
        el.disabled = false;
        if (document.getElementById("showLoading")) {
          el.removeChild(document.getElementById("showLoading"));
        }
        el.classList.remove("is-disabled");
      }, 2000);
    });
  },
})
/**
 * 自定义全局组件
 * */
import '@/components/index'

import * as filters from './filters' // global filters


// 登录主题 缓存中获取 没有则添加
let theme = localStorage.getItem('theme');
if (!theme) {
  localStorage.setItem('theme', 'default');
  window.document.documentElement.setAttribute("data-theme", 'default')
} else {
  window.document.documentElement.setAttribute("data-theme", theme)
}
// 百度地图
// import baiduMap from "vue-baidu-map";
// Vue.use(baiduMap, { ak: 'L7I2CnIRHUI1jZRDVUTDzG3Ixd06fY91' });
Vue.use(Element, {
  size: 'medium', // set element-ui default size
  zIndex: 3000,
  locale: {
    el: {
      ...zhLocale.el,
      pagination: { // / 自定义pagination文字
        pagesize: '条/页',
        total: `共计 {total} 条`
      }
    }
  }
})

// 全局loading
Vue.prototype.$validator = validator;
Vue.prototype.$_regExp = _regExp;

// 注册全局实用程序筛选器
Object.keys(filters).forEach(key => {
  Vue.filter(key, filters[key])
})

// 路由判断登录 根据路由配置文件的参数
router.beforeEach((to, from, next) => {
//  console.log(to, from, '---------- tf');
  let path = to.redirectedFrom || to.path
  if (path.indexOf('login') > 0) { // 用户主动走登录 让他走
    store.dispatch('user/SET_IS_ADD_ROUTES', false) // 清除掉添加过路由的判断 重新登录会重新添加路由 保证不会跳转404
    return next()
  } 
  // else {
  //   if (!store.getters.roleRouter) { // 用户没走首页 判断是否有动态路由 没有就走登录
  //     store.dispatch('user/SET_IS_ADD_ROUTES', false) // 清除掉添加过路由的判断 重新登录会重新添加路由 保证不会跳转404
  //     next({
  //       path: '/login',
  //       query: { redirect: to.fullPath }  // 将跳转的路由path作为参数，登录成功后跳转到该路由
  //     })
  //   }
  // }
  if (store.state.user.get_Token()) {//查看本地是否有token 没有说明没登录过 跳转login
    if (!store.getters.isAddRoutes) { // 存在角色路由 立即注册
      console.log('+_+++++++++++++++++++++++++++++++++++++++++++++++_-----------------------------------------');
      store.dispatch('user/SET_IS_ADD_ROUTES', true)
      // console.log(store.getters.roleRouter, '---------------- store.getters.roleRouter');
      router.addRoutes(store.getters.roleRouter)
      let currentItem = store.getters.roleRouter.findIndex(item => item.path == to.fullPath)
      next({path: currentItem.redirect || to.fullPath})
    } else {
      to.meta.breadTitle = to.query.title? to.query.title : to.meta.title
      next() //已经注册过动态路由 尝试匹配
    }
  } else {
    if(to.meta.isStaticRouter && to.fullPath =='/view') {
      next()
    }else{
      next({
        path: '/login'
      })
    }

  }
})


Vue.config.productionTip = false
let vm = new Vue({
  el: '#app',
  router,
  store,
  render: h => h(App),
  data: function () {
    return {
      isLoading: false,
      buttonLoading: false,
    }
  }
})
export default vm
